project(device_agent_wf_project)

cmake_minimum_required(VERSION 3.10)

find_package(PkgConfig REQUIRED)

pkg_check_modules(LUA REQUIRED lua)
pkg_check_modules(PROTOBUF REQUIRED protobuf)
pkg_check_modules(LOG4CPLUS REQUIRED log4cplus)
pkg_check_modules(JSONCPP REQUIRED jsoncpp)
pkg_check_modules(OPENSSL REQUIRED openssl)

set(CMAKE_C_COMPILER "clang")
set(CMAKE_CXX_COMPILER "clang++")
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin/)

set(CMAKE_CXX_FLAGS "-Wno-noexcept-type -Wno-dynamic-exception-spec -Wall -O2")
set(CMAKE_C_FLAGS "-Wno-comment -Wno-extern-initializer")

set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_STANDARD 17)

if (OE)
    add_definitions(-DOE)
endif()

set(SOURCE_DIRS
    ${CMAKE_SOURCE_DIR}/../entry
    ${CMAKE_SOURCE_DIR}/../control
    ${CMAKE_SOURCE_DIR}/../param
    ${CMAKE_SOURCE_DIR}/../agent
    
    #user code dirs
)

foreach(dir in ${SOURCE_DIRS})
    file(GLOB_RECURSE ALL_SUBDIRECTORIES LIST_DIRECTORIES true "${dir}/*")
    foreach(inc_dir in ${ALL_SUBDIRECTORIES})
        if (IS_DIRECTORY ${inc_dir})
            list(APPEND inc_dirs ${inc_dir})
        endif()
    endforeach()
endforeach()

set(MAVLINK_DIRS
        ${CMAKE_SOURCE_DIR}/../mavlink
)

foreach(dir in ${MAVLINK_DIRS})
    file(GLOB_RECURSE ALL_SUBDIRECTORIES LIST_DIRECTORIES true "${dir}/*")
    foreach(inc_dir in ${ALL_SUBDIRECTORIES})
        if (IS_DIRECTORY ${inc_dir})
            list(APPEND inc_dirs ${inc_dir})
        endif()
    endforeach()
endforeach()

list(APPEND inc_dirs ${SOURCE_DIRS})

option(DEVELOP_MODE "develop model" OFF)

if (DEVELOP_MODE)
    list(APPEND inc_dirs ${CMAKE_SOURCE_DIR}/../daf/include/)
    link_directories(${CMAKE_SOURCE_DIR}/../daf/lib64/)
endif()

include_directories(${inc_dirs})

foreach(dir in ${SOURCE_DIRS})
    file(GLOB_RECURSE SOURCES "${dir}/*.cpp" "${dir}/*.cc")
    list(APPEND ALL_SOURCES ${SOURCES})
endforeach()

set(BIN device_agent_wf)

add_executable(${BIN} ${ALL_SOURCES})
target_link_libraries(${BIN}
    daf
    gazella
    ${LUA_LIBRARIES}
    ${PROTOBUF_LIBRARIES}
    ${LOG4CPLUS_LIBRARIES}
    ${JSONCPP_LIBRARIES}
    ${OPENSSL_LIBRARIES}
    leveldb
    mavsdk
    pthread 
)
